package cn.kgc.controller;

import cn.kgc.pojo.Employment_dyb;
import cn.kgc.pojo.Organization;
import cn.kgc.pojo.Employment;
import cn.kgc.pojo.Position_dyb;
import cn.kgc.service.Service_dyb;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.omg.CORBA.INTERNAL;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

@Controller
public class Controller_dyb {

    @Resource
    private Service_dyb service_dyb;

    @RequestMapping("/a")
    public String showPosition(Model model) {
        return "职位发布管理";
    }

    @ResponseBody
    @RequestMapping("/show")
    public String showPosition(@RequestParam String positionName, @RequestParam String positionSort, @RequestParam String registranTime, @RequestParam String expirationDate,
                               @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Employment> employments = service_dyb.selEmployment(positionName, positionSort, registranTime, expirationDate);
        PageInfo<Employment> pageInfo = new PageInfo<Employment>(employments);
        return JSON.toJSONStringWithDateFormat(pageInfo, "yyyy-MM-dd");
    }

    @RequestMapping("/getDepartment")
    public String goPosition(Model model) {
        List<Organization> organization = service_dyb.getOrganization();
        model.addAttribute("list", organization);
        java.util.Date date = new java.util.Date();
        model.addAttribute("date",date);
        return "职位发布登记";
    }

    @RequestMapping("/getPosition")
    @ResponseBody
    public String getPositionById(@RequestParam Integer departmentid) {
        List<Position_dyb> positions = service_dyb.getPositionById(departmentid);
        return JSON.toJSONString(positions);
    }

    @RequestMapping("/savePositionInfo")
    @ResponseBody
    public String savePositionInfo(@ModelAttribute Employment_dyb employment_dyb, String expirationDates, String registranTimes) {
        employment_dyb.setExpirationDate(Date.valueOf(expirationDates));
        employment_dyb.setRegistranTime(Date.valueOf(registranTimes));
        Integer i = service_dyb.savePositionInfo(employment_dyb);
        return JSONObject.toJSONString(i);
    }

    @RequestMapping("/upPositionInfo")
    @ResponseBody
    public String upPositionInfo(@ModelAttribute Employment_dyb employment_dyb, String expirationDates, String registranTimes) {
        employment_dyb.setExpirationDate(Date.valueOf(expirationDates));
        employment_dyb.setRegistranTime(Date.valueOf(registranTimes));
        Integer i = service_dyb.upPositionInfo(employment_dyb);
        return JSONObject.toJSONString(i);
    }

    @RequestMapping("/goUpInfo")
    public String goUpInfo(@RequestParam Integer employmentId,@RequestParam Integer positionId,@RequestParam String allName, Model model){
        Employment_dyb employment = service_dyb.getEmploymentById(employmentId);
        List<Organization> organization = service_dyb.getOrganization();
        List<Position_dyb> positionList = service_dyb.getPositionByOrgan(allName);
//        Position_dyb position = service_dyb.getPosition(positionId);
//        System.out.println(position+"===================================================");
        model.addAttribute("positionlist",positionList);
        model.addAttribute("positionId",positionId);
        model.addAttribute("organization",organization);
        model.addAttribute("employment",employment);
        System.out.println(positionList+"===================================");
        System.out.println(positionId+"===================================");
//        model.addAttribute("position",position);
        return "职位变更登记";
    }



    @RequestMapping("/look")
    public String look(@RequestParam Integer employmentId,Model model){
        Employment_dyb employment = service_dyb.getEmploymentById(employmentId);
        model.addAttribute("employment",employment);
        return "职位明细";
    }

    @RequestMapping("/del")
    @ResponseBody
    public Integer del(@RequestParam Integer employmentId){
        Integer integer = service_dyb.delEmployment(employmentId);
        return integer;
    }
}
